//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2005
//
// 'Map properties' dialog, 'cgpsmapper' page.
//

# include "StdAfx.h"
# include "Resource.h"
# include "MapProperties.h"
# include "Map.h"
# include "ChangeMapHeader.h"
# include "ParseHelpers.h"
# include "LinkDlg.h"
# include "Polish.h"

INT_PTR CALLBACK _MapCGPSMapperProc (HWND _hWnd, UINT _uMsg, WPARAM _wParam, LPARAM _lParam) {
	const INT_PTR r = LinkDlgProc (_hWnd, _uMsg, _wParam, _lParam);
	if (r)
		return r;

	switch (_uMsg) {
		case WM_INITDIALOG: {
			CCritSec::CLock lock (g_cs);

			if (g_bPolish074)
				::CheckRadioButton (_hWnd, IDC_POLISH_074,   IDC_POLISH_074,   IDC_POLISH_074);
			else
				::CheckRadioButton (_hWnd, IDC_POLISH_RGNx0, IDC_POLISH_RGNx0, IDC_POLISH_RGNx0);

			char buf [64];

			// TreSize.
			::sprintf (buf, "%d", g_map.Header.cTreSize);
			::SetDlgItemText (_hWnd, IDC_MAP_TRESIZE, buf);

			// TreMargin.
			::sprintf (buf, "%0.3f", g_map.Header.fTreMargin);
			::SetDlgItemText (_hWnd, IDC_MAP_TREMARGIN, buf);

			// RgnLimit.
			::sprintf (buf, "%d", g_map.Header.cRgnLimit);
			::SetDlgItemText (_hWnd, IDC_MAP_RGNLIMIT, buf);

			// Preprocess.
			const HWND hPreprocess = ::GetDlgItem (_hWnd, IDC_MAP_PREPROCESS);
			::SendMessage (hPreprocess, CB_INSERTSTRING, 0, (LPARAM) "No");
			::SendMessage (hPreprocess, CB_SETITEMDATA,  0, map_header_t::ppNo);
			::SendMessage (hPreprocess, CB_INSERTSTRING, 1, (LPARAM) "Generalization only");
			::SendMessage (hPreprocess, CB_SETITEMDATA,  1, map_header_t::ppGeneralization);
			::SendMessage (hPreprocess, CB_INSERTSTRING, 2, (LPARAM) "Generalization + intersection");
			::SendMessage (hPreprocess, CB_SETITEMDATA,  2, map_header_t::ppFull);
			switch (g_map.Header.Preprocess) {
				case map_header_t::ppNo:             ::SendMessage (hPreprocess, CB_SETCURSEL, 0, 0); break;
				case map_header_t::ppGeneralization: ::SendMessage (hPreprocess, CB_SETCURSEL, 1, 0); break;
				case map_header_t::ppFull:           ::SendMessage (hPreprocess, CB_SETCURSEL, 2, 0); break;
				default: assert (false);
			}

			// Transparent.
			::CheckDlgButton (_hWnd, IDC_MAP_TRANSPARENT,    g_map.Header.bTransparent    ? BST_CHECKED : BST_UNCHECKED);

			// POI index.
			::CheckDlgButton (_hWnd, IDC_MAP_POIINDEX,       g_map.Header.bPOIIndex       ? BST_CHECKED : BST_UNCHECKED);
			::CheckDlgButton (_hWnd, IDC_MAP_POINUMBERFIRST, g_map.Header.bPOINumberFirst ? BST_CHECKED : BST_UNCHECKED);
			::CheckDlgButton (_hWnd, IDC_MAP_POIZIPFIRST,    g_map.Header.bPOIZipFirst    ? BST_CHECKED : BST_UNCHECKED);

			if (! g_map.Header.bPOIIndex) {
				::EnableWindow (::GetDlgItem (_hWnd, IDC_MAP_POINUMBERFIRST), FALSE);
				::EnableWindow (::GetDlgItem (_hWnd, IDC_MAP_POIZIPFIRST),    FALSE);
			}

			// Settings for commercial version of cgpsmapper.exe
			::CheckDlgButton (_hWnd, IDC_MAP_MG,             g_map.Header.bMG             ? BST_CHECKED : BST_UNCHECKED);
			::CheckDlgButton (_hWnd, IDC_MAP_NUMBERING,      g_map.Header.bNumbering      ? BST_CHECKED : BST_UNCHECKED);
			::CheckDlgButton (_hWnd, IDC_MAP_ROUTING,        g_map.Header.bRouting        ? BST_CHECKED : BST_UNCHECKED);

			return TRUE;
		}

		case WM_COMMAND: {
			switch (HIWORD (_wParam)) {
				case BN_CLICKED:
					PropSheet_Changed (::GetParent (_hWnd), _hWnd);

					if (LOWORD (_wParam) == IDC_MAP_POIINDEX) {
						if (::IsDlgButtonChecked (_hWnd, IDC_MAP_POIINDEX) == BST_CHECKED) {
							::EnableWindow (::GetDlgItem (_hWnd, IDC_MAP_POINUMBERFIRST), TRUE);
							::EnableWindow (::GetDlgItem (_hWnd, IDC_MAP_POIZIPFIRST),    TRUE);
						} else {
							::EnableWindow (::GetDlgItem (_hWnd, IDC_MAP_POINUMBERFIRST), FALSE);
							::EnableWindow (::GetDlgItem (_hWnd, IDC_MAP_POIZIPFIRST),    FALSE);
						}
					}
					break;
			}
			break;
		}

		case WM_NOTIFY: {
            const NMHDR * const pInfo = reinterpret_cast<NMHDR *> (_lParam);
            switch (pInfo->code) {
				case PSN_APPLY: {
					// Format.
					g_bPolish074 = ::IsDlgButtonChecked (_hWnd, IDC_POLISH_074) & BST_CHECKED;

					map_header_t Header = g_map.Header;

					char buf [256];

					// TreSize.
					::GetDlgItemText (_hWnd, IDC_MAP_TRESIZE,   buf, sizeof (buf)/sizeof (buf [0]));
					Header.cTreSize = ParseDecU (buf, buf + ::strlen (buf));

					// TreMargin.
					::GetDlgItemText (_hWnd, IDC_MAP_TREMARGIN, buf, sizeof (buf)/sizeof (buf [0]));
					Header.fTreMargin = ParseReal (buf, buf + ::strlen (buf));

					// RgnLimit.
					::GetDlgItemText (_hWnd, IDC_MAP_RGNLIMIT,  buf, sizeof (buf)/sizeof (buf [0]));
					Header.cRgnLimit = ParseDecU (buf, buf + ::strlen (buf));

					// Preprocess.
					const HWND hPreprocess = ::GetDlgItem (_hWnd, IDC_MAP_PREPROCESS);
					const size_t cPreprocessSel = ::SendMessage (hPreprocess, CB_GETCURSEL, 0, 0);
					if (cPreprocessSel != CB_ERR)
						Header.Preprocess = static_cast<map_header_t::preprocess_t> (::SendMessage (hPreprocess, CB_GETITEMDATA, cPreprocessSel, 0));

					// Transparent.
					Header.bTransparent    = ::IsDlgButtonChecked (_hWnd, IDC_MAP_TRANSPARENT)    == BST_CHECKED;

					// POI index.
					Header.bPOIIndex       = ::IsDlgButtonChecked (_hWnd, IDC_MAP_POIINDEX)       == BST_CHECKED;
					Header.bPOINumberFirst = ::IsDlgButtonChecked (_hWnd, IDC_MAP_POINUMBERFIRST) == BST_CHECKED;
					Header.bPOIZipFirst    = ::IsDlgButtonChecked (_hWnd, IDC_MAP_POIZIPFIRST)    == BST_CHECKED;

					Header.bMG             = ::IsDlgButtonChecked (_hWnd, IDC_MAP_MG)             == BST_CHECKED;
					Header.bNumbering      = ::IsDlgButtonChecked (_hWnd, IDC_MAP_NUMBERING)      == BST_CHECKED;
					Header.bRouting        = ::IsDlgButtonChecked (_hWnd, IDC_MAP_ROUTING)        == BST_CHECKED;

					if (! (Header == g_map.Header))
						ChangeMapHeader (Header);

					break;
				}
			}
			break;
		}
	}

	return FALSE;
}
